<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            list-style: none;
        }
        
        .box {
            width: 300px;
            /* height: 200px; */
            border: 1px solid #000;
            margin: auto;
        }
        
        .box>.head {
            display: flex;
            justify-content: space-between;
        }
        
        .box>.head>li {
            flex-grow: 1;
            text-align: center;
            cursor: pointer;
        }
        
        .active {
            background-color: skyblue;
        }
        
        .main>div {
            display: none;
        }
        
        .main>.main1 {
            display: block;
        }
        
        .main {
            height: 200px;
            background-color: pink;
        }
    </style>
</head>

<body>
    <div class="box">
        <ul class="head">
            <li class="active">标题一</li>
            <li>标题二</li>
            <li>标题三</li>
        </ul>

        <div class="main">
            <div class="main1">
                内容一
            </div>
            <div>
                内容二
            </div>
            <div>
                内容三
            </div>
        </div>
    </div>

    <script>
        let list = document.querySelectorAll(".head > li");
        let main = document.querySelector(".main")
        let threeDiv = document.querySelectorAll('.main>div')
        console.log(threeDiv);
        let newthreeDiv = [];
        let lastIndex = 0; // 设置上一步操作的下标

        //  head容器每个li绑定事件
        list.forEach(function(item, index) {
            // console.log(item);
            item.onclick = function() {
                // li里面的active类名全部移除
                // list.forEach(function (item) {
                //     item.classList.remove("active");
                // });

                // 清除上一个操作的类名
                list[lastIndex].classList.remove("active");
                // 当前li操作添加类名
                item.classList.add("active");

                // 点击更换内容
                // main.innerText = item.innerText;

                // 作业(不同的标题对应不同的容器)
                threeDiv.forEach(function(item, index) {
                    // console.log(item);
                    newthreeDiv.push(item);
                    // console.log(newthreeDiv);
                })
                newthreeDiv[lastIndex].classList.remove('main1');
                newthreeDiv[index].classList.add('main1');
                main.appendChild(newthreeDiv[index])
                    // 保存操作完毕之后的下标
                lastIndex = index;
            }
        });
    </script>
</body>

</html>